<html>
<head>

<script src="sorttable.js"></script>
<style type="text/css">
table.sortable a.sortheader {
    background-color:#eee;
    color:#666666;
    font-weight: bold;
    text-decoration: none;
    display: block;
}
table.sortable span.sortarrow {
    color: black;
    text-decoration: none;
}
</style>
<script src="chemdraw/chemdraw.js"></script>
<script>
  cd_includeWrapperFile("chemdraw/"); 
</script>


<title>RDKit XPCOM Demo</title>
</head>

<body>


<script>
function getRpcClient() {
  return Components.classes['@mozilla.org/xml-rpc/client;1']
     .createInstance(Components.interfaces.nsIXmlRpcClient);
};

var xmlRpcClient;
var xmlRpcClientInited;
function getXmlRpc() {
  if (!xmlRpcClient) xmlRpcClient = getRpcClient();
  return xmlRpcClient;
}

var Listener = {
    onResult: function(client, ctxt, result) {
      netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
      //result = result.QueryInterface(Components.interfaces.nsISupportsCString);
    },
    onFault: function(client, ctxt, fault) {
        alert('Fault! ' + fault + '\n');
    },
    onError: function(client, ctxt, status, errorMsg) {
        alert('Error! <(' + status.toString(16) + ') ' + errorMsg + '>\n');
    }
};

netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var cls = Components.classes["@rationaldiscovery.com/RDKit/base"].createInstance();
var baseI = cls.QueryInterface(Components.interfaces.IRDKit);

function formatNumber(number, numDecimals) { 
  var str = number.toString() 
  var point = str.indexOf(".") 
  var newstring = str.substring(0, Number(point) + Number(numDecimals) + 1);
  return(Number(newstring)) 
} 

function addMolToTable(table,mol){
  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  var canSmi = mol.GetSmiles();
  var numDonors=mol.GetSmartsMatchCount('[$([#7,#8;!H0])]');
  var numAcceptors=mol.GetSmartsMatchCount('[$([#7,#8])]');
  var numRotatable=mol.GetSmartsMatchCount('[!$(*#*)&!D1]-&!@[!$(*#*)&!D1]')
  var mw=mol.GetMW();
  var logp=mol.LogP();
  var mr = mol.MR();
  var url="data:chemical/smiles,"+canSmi;
  var row = table.insertRow(table.rows.length);
  var img = cd_getSpecificObjectTag("chemical/x-cdx",150,150,"cdp"+String(table.rows.length),"",true,true,url);

  var numViolations=0;
  if(mw>500) numViolations++;
  if(logp>5) numViolations++;
  if(numDonors>5) numViolations++;
  if(numAcceptors>10) numViolations++;
  var cellIdx=0;
  row.insertCell(cellIdx++).innerHTML=img;
  row.insertCell(cellIdx++).innerHTML=numViolations;
  row.insertCell(cellIdx++).innerHTML=formatNumber(mw,3);
  row.insertCell(cellIdx++).innerHTML=formatNumber(logp,4);
  row.insertCell(cellIdx++).innerHTML=numDonors;
  row.insertCell(cellIdx++).innerHTML=numAcceptors;
  row.insertCell(cellIdx++).innerHTML=numRotatable;
  row.insertCell(cellIdx++).innerHTML=formatNumber(mr,4);
  row.insertCell(cellIdx++).innerHTML=canSmi;
};

function addSmiles(smiles)
{
  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  var mol = baseI.MolFromSmiles(smiles);
  var tbl = document.getElementById('moltable');
  //addMolToTable(tbl,mol);
  var c = getXmlRpc();
  /*if(!xmlRpcClientInited) c.init("http://localhost:9123");*/
  //mb = mol.GetMolBlock()

  c.init("http://localhost:9123");
  c.asyncCall(Listener,null,"ping",[],0);
  //c.init('http://www.xmlrpc.com/RPC2');
  var stateCode= c.createType(c.INT,{});
  stateCode.data=41;
  //c.asyncCall(Listener,null,"examples.getStateName",[stateCode],1);
  
  //c.asyncCall(Listener,null,"loadMolBlock",[mb,"foob"],0);
  //c.asyncCall(Listener,null,"ping",[],0);
}

function molblockToPyMol(mb,id){
  var pymol = getXmlRpc();
  pymol.init("http://localhost:9123");
  var mbVar=pymol.createType(pymol.STRING,{});
  mbVar.data=mb;
  var idVar = pymol.createType(pymol.STRING,{});
  idVar.data=id;
  pymol.asyncCall(Listener,null,"loadMolBlock",[mbVar,idVar],2);
}

function addSDFile(fileName)
{
  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  var suppl = baseI.SupplierFromSDFile(fileName);
  var tbl = document.getElementById('moltable');
  var mol;
  while(!suppl.atEnd()){
    mol = suppl.next();
    addMolToTable(tbl,mol);
  }
  
  var mb = mol.GetMolBlock();
  molblockToPyMol(mb,"foob");
}


</script>

<form>
<table>
<tr>
  <td>
    <input name="textBox" cols=80 value="c1cc(N)ccc1CC(=O)O">
    <input type=button onclick="addSmiles(form.textBox.value)" value="Grab">
  </td>
</tr>
<tr>
  <td>
    <input type=file name="sdBox" cols=80>
    <input type=button onclick="addSDFile(form.sdBox.value)" value="Grab SD">
  </td>
</tr>
</table>

<br>
<table class="sortable" id="moltable" border=1>
  <tr>
    <th></th>
    <th>Violations</th>
    <th>MW</th>
    <th>LogP</th>
    <th>NumDonors</th>
    <th>NumAcceptors</th>
    <th>NumRotatable</th>
    <th>MR</th>
    <th>smiles</th>
  </tr>
</table>

</form>


</body>
</html>
